<?php
defined( '_VALID_MOS' ) or die();

// ------------------  standard plugin initialize function - don't change ---------------------------
global $sh_LANG, $sefConfig;
$shLangName = '';
$shLangIso = '';
$title = array();
$shItemidString = '';
$dosef = shInitializePlugin($lang, $shLangName, $shLangIso, $option);
// ------------------  standard plugin initialize function - don't change ---------------------------

// ------------------  load language file - adjust as needed ----------------------------------------
$shLangIso = shLoadPluginLanguage('com_boss', $shLangIso, '_COM_BOSS_SEF_CONTENT');
// ------------------  load language file - adjust as needed ----------------------------------------

// remove common URL from GET vars list, so that they don't show up as query string in the URL
shRemoveFromGETVarsList('option');
shRemoveFromGETVarsList('lang');
if (!empty($Itemid)) shRemoveFromGETVarsList('Itemid');
if (!empty($limit)) shRemoveFromGETVarsList('limit');
if (isset($limitstart)) shRemoveFromGETVarsList('limitstart'); // limitstart can be zero

// start by inserting the menu element title (just an idea, this is not required at all)

$task = isset($task) ? @$task : null;
$Itemid = isset($Itemid) ? @$Itemid : null;
$text_search = isset($text_search) ? @$text_search : null;
$order = isset($order) ? @$order : null;
$expand = isset($expand) ? @$expand : null;
$userid = isset($userid) ? @$userid : null;

//$shAdsManagerName = shGetComponentPrefix($option);
//$shAdsManagerName = empty($shAdsManagerName) ? getMenuTitle($option, $task, $Itemid, null, $shLangName) : $shAdsManagerName;

if (!$expand) shRemoveFromGETVarsList('expand');
if (!$order) shRemoveFromGETVarsList('order');
//if ($text_search==0) shRemoveFromGETVarsList('text_search');

//$title[] = $shAdsManagerName;

//require_once(JPATH_BASE . '/components/com_boss/lang/russian.php');

//Limitstart, limit nog toevoegen
$title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_CONTENT'];

extract($vars);

$catid = (isset($vars['catid'])) ? @$vars['catid'] : 0;
$directory = (isset($vars['directory'])) ? @$vars['directory'] : 0;
$contentid = (isset($vars['contentid'])) ? @$vars['contentid'] : 0;
$alpha = (isset($vars['alpha'])) ? @$vars['alpha'] : '';
$dirName = $database->setQuery("SELECT slug FROM #__boss_config WHERE id = $directory LIMIT 1")->loadResult();
$current_list = array();

switch ($task) {

    case 'show_profile':
        {
        $title[] = $dirName;
        $title[] = $userid;
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_EDIT'] . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('userid');

        //$title[] = BOSS_SEF_PROFILE.$vars['userid']."/".BOSS_SEF_EDIT.$sefConfig->suffix;
        break;
        }

    case 'save_profile':
        {
        $title[] = $dirName;
        $title[] = $userid;
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_PROFILE'] . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('userid');

        //$title[] = BOSS_SEF_PROFILE.$vars['userid']."/".BOSS_SEF_SAVE.$sefConfig->suffix;
        break;
        }

    case 'show_user':
        {
        if (isset($userid)) {
            $title[] = $dirName;
            $title[] = $userid;
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_USER'] . $sefConfig->suffix;
            shRemoveFromGETVarsList('userid');
        } else {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_MY_CONTENT'] . $sefConfig->suffix;
        }
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'show_category':
        {
        $title[] = $dirName;

        if ($catid != 0) {
            $database->setQuery("SELECT c.* FROM #__boss_" . $directory . "_categories as c " .
                                "WHERE c.published = 1 ORDER BY c.parent,c.ordering");

            $rows = $database->loadObjectList();
            if ($database->getErrorNum()) {
                echo $database->stderr();
                return false;
            }

            // establish the hierarchy of the menu
            $orderlist = array();


            // first pass - collect children
            foreach ($rows as $v) {
                $orderlist[$v->id] = $v;
            }

            $current = $catid;
            while (@$orderlist[$current]->id != 0)
            {
                $current_list[] = $orderlist[$current]->slug;
                $current = $orderlist[$current]->parent;
            }
        }

        $size = count($current_list);
        for ($i = $size - 1; $i > 0; $i--)
        {
            $title[] = $current_list[$i];
        }
        $tmp = '';
        if(isset($current_list[0]))
            $tmp .= $current_list[0];
        if($order && isset($current_list[0]))
            $tmp .= '-';
        if($order)
            $tmp .= $order;

        $title[] = $tmp . $sefConfig->suffix;
        unset($tmp);
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('catid');
        shRemoveFromGETVarsList('slug');
        shRemoveFromGETVarsList('order');
        shRemoveFromGETVarsList('text_search');
        break;
        }

    case 'search_alpha':
        {
        $title[] = $dirName;

        if ($catid != 0) {
            $database->setQuery("SELECT c.* FROM #__boss_" . $directory . "_categories as c " .
                                "WHERE c.published = 1 ORDER BY c.parent,c.ordering");

            $rows = $database->loadObjectList();
            if ($database->getErrorNum()) {
                echo $database->stderr();
                return false;
            }

            // establish the hierarchy of the menu
            $orderlist = array();


            // first pass - collect children
            foreach ($rows as $v) {
                $orderlist[$v->id] = $v;
            }

            $current = $catid;
            while (@$orderlist[$current]->id != 0)
            {
                $current_list[] = $orderlist[$current]->slug;
                $current = $orderlist[$current]->parent;
            }
        }

        $size = count($current_list);
        for ($i = $size - 1; $i > 0; $i--)
        {
            $title[] = $current_list[$i];
        }
        $tmp = '';
        if(isset($current_list[0]))
            $tmp .= $current_list[0];
        if($order && isset($current_list[0]))
            $tmp .= '-';
        if($order)
            $tmp .= $order;

        if($tmp)
            $title[] = $tmp;

        $title[] = 'alpha-'. $alpha . $sefConfig->suffix;
        unset($tmp);
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('catid');
        shRemoveFromGETVarsList('slug');
        shRemoveFromGETVarsList('order');
        shRemoveFromGETVarsList('text_search');
        shRemoveFromGETVarsList('alpha');
        break;
        }

    case 'show_rules':
        {
        $title[] = $dirName;
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_RULES'] . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'show_content':
        {
        $title[] = $dirName;
        $database->setQuery("SELECT name, slug FROM #__boss_" . $directory . "_contents WHERE id=$contentid LIMIT 1");
        $database->loadObject($text);
        $text = (!empty($text->slug)) ? $text->slug : $text->name;

        $database->setQuery("SELECT c.* FROM #__boss_" . $directory . "_categories as c " .
                            "WHERE c.published = 1 ORDER BY c.parent,c.ordering");

        $rows = $database->loadObjectList();
        if ($database->getErrorNum()) {
            echo $database->stderr();
            return false;
        }

        // establish the hierarchy of the menu
        $orderlist = array();
        // first pass - collect children
        foreach ($rows as $v) {
            $orderlist[$v->id] = $v;
        }

        $current = $catid;
        if (count($orderlist) > 0) {
            while (@$orderlist[$current]->id != 0)
            {
                $current_list[] = $orderlist[$current]->slug;
                $current = $orderlist[$current]->parent;
            }
        }

        $size = count($current_list);
        for ($i = $size - 1; $i > 0; $i--)
        {
            $title[] = $current_list[$i];
        }
        if (count($current_list) > 0)
            $title[] = $current_list[0];

        $text = substr($text, 0, 90);

        $title[] = $text . "-" . $contentid . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('contentid');
        shRemoveFromGETVarsList('catid');
        shRemoveFromGETVarsList('slug');
        break;
        }

    case 'write_content':
        {
        $title[] = $dirName;
        if ($contentid == 0) {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_WRITE_CONTENT'] . "-" . @$vars['catid'] . $sefConfig->suffix;
        }
        else {
            $database->setQuery("SELECT slug FROM #__boss_" . $directory . "_contents WHERE id=$contentid LIMIT 1");
            $text = $database->loadResult();
                var_dump($catid);
            $database->setQuery("SELECT slug, parent FROM #__boss_" . $directory . "_categories WHERE published='1' AND id=$catid LIMIT 1");

            $rows_categories = $database->loadObjectList();
            $cat_name = $rows_categories[0]->slug;
            $parentid = $rows_categories[0]->parent;

            if ($parentid != 0) {
                $parent = $rows_categories[0]->parent;
                $database->setQuery("SELECT slug, FROM #__boss_" . $directory . "_categories WHERE published='1' AND id=$parent LIMIT 1");
                $parent_name = $database->loadResult();
                var_dump($parent_name);
                $title[] = $parent_name;
            }

            $title[] = $cat_name;

            $text = substr($text, 0, 90);

            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_UPDATE'] . "-" . $text . "-" . $contentid . $sefConfig->suffix;
            shRemoveFromGETVarsList('contentid');
        }
        shRemoveFromGETVarsList('catid');
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('slug');
        break;
        }

    case 'save_content':
        {
        $title[] = $dirName;
        if ($contentid == 0) {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SAVE_CONTENT'] . $sefConfig->suffix;
        }
        else
        {
            $database->setQuery("SELECT slug FROM #__boss_" . $directory . "_contents WHERE id=$contentid LIMIT 1");
            $text = $database->loadResult();


            $database->setQuery("SELECT slug, parent FROM #__boss_" . $directory . "_categories WHERE published='1' AND id=$catid LIMIT 1");

            $rows_categories = $database->loadObjectList();
            $cat_name = $rows_categories[0]->slug;
            $parentid = $rows_categories[0]->parent;

            if ($parentid != 0) {
                $parent = $rows_categories[0]->parent;
                $database->setQuery("SELECT slug, FROM #__boss_" . $directory . "_categories WHERE published='1' AND id=$parent LIMIT 1");

                $rows_categories = $database->loadObjectList();
                $parent_name = $rows_categories[0]->slug;
                $title[] = $parent_name;
            }

            $title[] = $cat_name;

            $text = substr($text, 0, 90);

            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SAVE'] . "-" . $text . "-" . $contentid . $sefConfig->suffix;
            shRemoveFromGETVarsList('contentid');
            shRemoveFromGETVarsList('catid');
        }
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('slug');
        break;
        }

    case 'delete_content':
        {
        $title[] = $dirName;
        if ($contentid == 0) {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_DELETE_CONTENT'] . $sefConfig->suffix;
        }
        else
        {
            $database->setQuery("SELECT slug FROM #__boss_" . $directory . "_contents WHERE id=$contentid LIMIT 1");
            $text = $database->loadResult();
            $text = substr($text, 0, 90);

            $mode = (isset($vars['mode'])) ? "-" . @$vars['mode'] : '';

            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_DELETE'] . "-" . $text . "-" . $contentid . $mode . $sefConfig->suffix;
            shRemoveFromGETVarsList('catid');
            shRemoveFromGETVarsList('contentid');
        }
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('slug');
        break;
        }

    case 'show_all':
        {
        $title[] = $dirName;
        $limitstart = (isset($vars['limitstart'])) ? "-" . @$vars['limitstart'] : '';
        $text_search = (isset($vars['text_search'])) ? "-" . @$vars['text_search'] : '';
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_ALL_CONTENT'] . $text_search . $limitstart . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('text_search');
        break;
        }

    case 'search':
        {
        $title[] = $dirName;
        $limitstart = (isset($vars['limitstart'])) ? "-" . @$vars['limitstart'] : '';
        $text_search = (isset($vars['text_search'])) ? "-" . @$vars['text_search'] : '';
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SEARCH'] . $text_search . $limitstart . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'show_search':
        {
        $title[] = $dirName;
        if (isset($vars['catid'])) {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SHOW_SEARCH'] . "-" . $vars['catid'] . $sefConfig->suffix;
        } else {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SHOW_SEARCH'] . $sefConfig->suffix;
        }
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'show_result':
        {
        $title[] = $dirName;
        if ($catid == 0) {
            $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_ALL_CONTENT'];
        }
        else {
            $title[] = $database->setQuery("SELECT slug FROM #__boss_" . $directory . "_categories WHERE id=$catid LIMIT 1")->loadResult();
        }
        $title[] = $sh_LANG[$shLangIso]['_COM_BOSS_SEF_SHOW_RESULT'] . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('catid');
        break;
        }

    case 'show_expiration':
        {
        $title[] = $dirName;
        $title[] = "show_expirations-" . $contentid . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'expiration':
        {
        $title[] = $dirName;
        $title[] = "expirations-" . $contentid . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'show_message_form':
        {
        $title[] = $dirName;
        $title[] = "message-" . $contentid . "-" . $vars['mode'] . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('mode');
        shRemoveFromGETVarsList('contentid');
        shRemoveFromGETVarsList('directory');
        break;
        }

    case 'send_message':
        {
        $title[] = $dirName;
        $mode = $vars['mode'];
        $title[] = "message-send-" . $mode . $sefConfig->suffix;
        shRemoveFromGETVarsList('task');
        shRemoveFromGETVarsList('directory');
        shRemoveFromGETVarsList('mode');
        break;
        }

    default:
        {
        $title[] = $dirName;
        $title[] = "boss" . $sefConfig->suffix;
        break;
        }
}

// ------------------  standard plugin finalize function - don't change ---------------------------
if ($dosef) {
    $string = shFinalizePlugin($string, $title, $shAppendString, $shItemidString,
        (isset($limit) ? @$limit : null), (isset($limitstart) ? @$limitstart : null),
        (isset($shLangName) ? @$shLangName : null));
    // ------------------  standard plugin finalize function - don't change ---------------------------
} else {
    //echo sef_404::sefGetLocation($string, $title);
    $string = sef_404::sefGetLocation($string, $title);
}
?>